import { defineStore } from 'pinia'
import { reactive, ref } from 'vue'
import { get, post } from '@/utils/api'
import { MessagePlugin } from 'tdesign-vue-next'

export const useAdminCopyrightStore = defineStore('adminCopyright', () => {
  const copyrightData = reactive({
    id: 1,
    copyright: '',
    icp: ''
  })

  const getConfig = async () => {
    const response = await get('/api/copyright/getConfig')
    Object.assign(copyrightData, response)
  }

  const saveConfig = async () => {
    try {
      loading.value = true
      const response = await post('/api/copyright/saveConfig', copyrightData)
      loading.value = false
      MessagePlugin.closeAll()
      MessagePlugin(response.status, response.message)
      await getConfig()
    } catch (err) {
      console.log(err)
    }
  }
  const loading = ref<boolean>(false)
  const contentLoading = ref<boolean>(true)

  return {
    copyrightData,
    saveConfig,
    loading,
    contentLoading,
    getConfig
  }
})
